<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <script src="/static/js/jquery-1.8.2.min.js"></script>

    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">


    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>

    <style>
        body {
            background: #93defe;
            background-size: cover;
        }
    </style>
</head>
<body>
<div class="jumbotron" style="width: 1200px;margin:150px auto">

    <form class="form-horizontal" method="post" enctype="multipart/form-data" action="" id="myform">

        <div class="form-group">
            <label for="inputUsername3" class="col-sm-2 control-label">用户名：</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputUsername3" placeholder="username" name="username">
                <span style="display: none;color: red"></span>
            </div>
        </div>

        <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">密码：</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" id="inputPassword3" placeholder="Password" name="password">
                <span style="display: none;color: red"></span>
            </div>
        </div>


        <div class="form-group">
            <label for="nickname" class="col-sm-2 control-label">昵称：</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="nickname" placeholder="nickname" name="nickname">
            </div>
        </div>
        <div class="form-group">
            <label for="nickname" class="col-sm-2 control-label">性别：</label>
            <div class="col-sm-10">
                <input type="radio" name="gender" placeholder="nickname" value="男">男
                <input type="radio" name="gender" placeholder="nickname" value="女">女
            </div>
        </div>
        <div class="form-group">
            <label for="sign" class="col-sm-2 control-label">个性签名：</label>
            <div class="col-sm-10">
                <textarea class="form-control" rows="3" name="sign" id="sign"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label for="exampleInputFile" class="col-sm-2 control-label">上传头像：</label>
            <div class="col-sm-10">
                <input type="file" id="exampleInputFile" name="pic">

            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default" onclick="return false"> 注册</button>
                <span style="color: red;display: none"></span>
            </div>
        </div>
    </form>

</div>

{% if success %}

    <script>
        window.alert("注册失败")
    </script>

{% endif %}

<script>

    $(function () {

        error_username = false;
        error_password = false;

        $("#inputUsername3").blur(function () {
            check_name();
        });

        $("#inputPassword3").blur(function () {
            check_password();
        });


        function check_name() {
            //用户名是否合法（8-16位字符组成，不能是纯数字，或字母）
            var username = $("#inputUsername3").val();
            var len = username.length;
            var reg = /^[a-zA-Z]*$/g;

            console.log(!reg.test(username));
            console.log(isNaN(username));  //返回true 不是纯数字


            if (len > 8 && len < 16 && isNaN(username) && !reg.test(username)) {
                $("#inputUsername3").next().hide();
                $.post("/check_username/", {"username": username}, function (data) {
                    //回调函数
                    if (data.success == 1) {
                        error_username = false;
                        $("#inputUsername3").next().show().html(data.msg)
                    } else {
                        error_username = true;
                        $("#inputUsername3").next().hide()
                    }
                })

            } else {
                error_username = false;
                $("#inputUsername3").next().show().html('8-16位字符组成，不能是纯数字，或字母')
            }

        }

        function check_password() {
            var password = $("#inputPassword3").val();
            var reg = /^[a-zA-Z0-9]{8,16}/;

            if (reg.test(password)) {
                error_password = true;
                $("#inputPassword3").next().hide()
            } else {
                error_password = false;
                $("#inputPassword3").next().show().html("密码为8,16字母和数字组成")
            }
        }


        $(":submit").click(function () {
            check_name();
            check_password();
            if (error_username && error_password) {
                //注册
                //需要一个一个input表单获取数据
                {#$.post('/register/',{})#}

                //封装form 表单里的所有数据，发送给后台
                var formdata = new FormData(document.getElementById("myform"));

                $.ajax({
                        type: "post",
                        url: '/register/',
                        data: formdata,
                        processData: false,
                        contentType: false,
                        success: function (data) {
                            //发送成功的回调函数
                            if (data.success == 0) {
                                window.location.href = data.url
                            } else {
                                $(":submit").next().show().html(data.msg)
                            }
                        }
                    }
                )
            }

        })

    })

</script>


</body>
</html>